package com.huawei.hianalytics.log.e;

import android.content.Context;
import com.huawei.hianalytics.g.b;
import com.huawei.hianalytics.util.c;
import com.huawei.hms.framework.network.restclient.dnkeeper.DNKeeperConfig;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class a {
    public static synchronized void a(Context context) {
        synchronized (a.class) {
            if (context != null) {
                b.b("HiAnalytics/logServer", "Clear all data of local log");
                b(new File(context.getFilesDir().getPath() + com.huawei.hianalytics.log.b.a.a.a));
            }
        }
    }

    public static void a(File file) {
        c.a(file);
    }

    public static void a(String str) {
        File[] listFiles = new File(str).listFiles();
        if (listFiles != null) {
            for (File file : listFiles) {
                String name = file.getName();
                if (name.endsWith(".log") || name.endsWith(".zip")) {
                    if (!"eventinfo.log".equals(name)) {
                        String[] split = name.substring(0, name.lastIndexOf(".")).split("_");
                        if (System.currentTimeMillis() - b(split[split.length - 1].trim()) >= DNKeeperConfig.SEVEN_DAYS && file.delete()) {
                            b.b("HiAnalytics/logServer", "out time file del ok");
                        }
                    }
                } else if (file.delete()) {
                    b.b("HiAnalytics/logServer", "this file is not our fileName :" + name);
                }
            }
        }
    }

    private static long b(String str) {
        long j = 0;
        try {
            Date parse = new SimpleDateFormat("yyyyMMddHHmmssSSS", Locale.getDefault()).parse(str);
            if (parse != null) {
                j = parse.getTime();
            }
        } catch (ParseException e) {
            b.c("HiAnalytics/logServer", "Time conversion Exception : getTimeMillis!");
        }
        return j;
    }

    public static void b(File file) {
        if (file == null || !file.exists()) {
            return;
        }
        if (file.isFile()) {
            if (!file.delete()) {
                b.c("HiAnalytics/logServer", "refresh clear file fail");
            }
        } else if (file.isDirectory()) {
            String[] list = file.list();
            if (list != null) {
                for (String file2 : list) {
                    b(new File(file, file2));
                }
                if (!file.delete()) {
                    b.c("HiAnalytics/logServer", "refresh clear file fail");
                }
            } else if (!file.delete()) {
                b.c("HiAnalytics/logServer", "refresh clear file fail");
            }
        }
    }
}
